package com.hmyd.hmyd_android.ui;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.widget.TextView;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.adapter.RechargeHistoryListAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Deposit;
import com.hmyd.hmyd_android.dto.DepositHistory;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.listview.XListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 充值历史
 */
@ContentView(R.layout.activity_recharge_history_list)
public class RechargeHistoryListActivity extends BasicListActivity {
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;
	
	@ViewInject(R.id.list)
	private XListView mList;
	private RechargeHistoryListAdapter adapter;
	@ViewInject(R.id.total)
	private TextView total;
	
	// 充值数据
	private List<DepositHistory> plist = new ArrayList<DepositHistory>();
	
	private final int REFRESH = 1;
	private final int LOADMORE = 2;
	private final int TOTALAMOUNT = 3;
	
	protected boolean hasMore = false;
	private String cursorId;
	
	@Override
	public XListView getXList() {
		return mList;
	}
	
	/**
	 * 发送获取数据请求
	 */
	private void onRequest() {
		onRequest(REFRESH, UrlConstant.DEPOSITHISTORY, HttpMethod.GET, null);
		onRequest(TOTALAMOUNT, UrlConstant.TOTALAMOUNT, HttpMethod.GET, null);
	}
	
	/**
	 * 刷新数据
	 */
	@Override
	public void onRefresh() {
		super.onRefresh();
		
		onRequest(REFRESH, UrlConstant.DEPOSITHISTORY, HttpMethod.GET, null);
		onRequest(TOTALAMOUNT, UrlConstant.TOTALAMOUNT, HttpMethod.GET, null);
	}
	
	/**
	 * 加载更多
	 */
	@Override
	public void onLoadMore() {
		super.onLoadMore();
		
		RequestParams params = new RequestParams();
		params.addBodyParameter("lastCursor", cursorId);
		
		onRequest(LOADMORE, UrlConstant.DEPOSITHISTORY, HttpMethod.GET, params);
	}
	
	@Override
	public void onPostSuccess(int postId, String msg) {
		super.onPostSuccess(postId, msg);

		Gson gson = new Gson();
		
		if (postId == TOTALAMOUNT) {
			Type totalType = new TypeToken<Deposit>() {}.getType();

			Deposit deposit = gson.fromJson(msg, totalType);

			total.setText(Utils.amountConversionFormat(deposit.totalAmount));
			return;
		}


		Type historyType = new TypeToken<Page<DepositHistory>>() {}.getType();

		Page<DepositHistory> page = gson.fromJson(msg, historyType);

		hasMore = page.hasMore;

		if(page!=null && page.records!=null){
			switch (postId) {
			case REFRESH:
				plist.clear();
				plist.addAll(page.records);
				break;
			case LOADMORE:
				plist.addAll(page.records);
				break;
			default:
				break;
			}

			if(plist.size()>0){
				cursorId = String.valueOf(plist.get(plist.size()-1).createdTime);
			}else{
				cursorId = null;
			}

			adapter.notifyDataSetChanged();
		}

		if(getXList()!=null && hasMore){
			getXList().setPullLoadEnable(true);
		}else if(getXList()!=null && !hasMore){
			getXList().setPullLoadEnable(false);
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		super.onPostFailure(postId, msg);
		
		if(getXList()!=null && hasMore){
			getXList().setPullLoadEnable(true);
		}else if(getXList()!=null && !hasMore){
			getXList().setPullLoadEnable(false);
		}
	}

	@Override
	protected void setParams() {
		home_title_txt.setText(getString(R.string.assets_recharge_detail));
		home_title.setOnTouchListener(touch);
		
		mList.setPullRefreshEnable(true); // 允许下拉刷新
		mList.setPullLoadEnable(false); // 禁止加载更多
		mList.setAutoLoadEnable(false); // 禁止自动加载
		mList.setXListViewListener(this);// 加载监听
		
		adapter = new RechargeHistoryListAdapter(this, plist);
		
		mList.setAdapter(adapter);
		
		onRequest();
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishActivity(this);
	}

	@Override
	protected void rightButton() {

	}
}
